<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Asynchronous Event Testing</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: Asynchronous Event Testing</h1>

    
        <a href="#toc" class="jump">Jump to Table of Contents</a>
    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
    <p>This example shows how to create an asynchronous test with the YUI Test framework for testing browser-based JavaScript code.
      A <code>Y.Test.Case</code></a> object is created to test the
      <code>Y.Anim</code> object. The test waits until the animation is complete
      before checking the settings of the animated element.</p>
</div>

<div class="example yui3-skin-sam" style="position: relative;">
    <style scoped>
    #testLogger {
        margin-bottom: 1em;
    }

    #testLogger .yui3-console .yui3-console-title {
        border: 0 none;
        color: #000;
        font-size: 13px;
        font-weight: bold;
        margin: 0;
        text-transform: none;
    }
    #testLogger .yui3-console .yui3-console-entry-meta {
        margin: 0;
    }

    .yui3-skin-sam .yui3-console-entry-pass .yui3-console-entry-cat {
        background: #070;
        color: #fff;
    }
    </style>

    <div id="testLogger"></div>
<div id="testDiv" style="left:0;position:absolute;width:10px;height:10px; background-color:red"></div>

<script>
YUI().use('anim', 'console', 'test', function (Y) {

    Y.namespace("example.test");

    Y.example.test.AsyncTestCase = new Y.Test.Case({

        //name of the test case - if not provided, one is auto-generated
        name : "Animation Tests",

        //---------------------------------------------------------------------
        // Test methods - names must begin with "test"
        //---------------------------------------------------------------------

        testAnimation : function (){

            var myAnim = new Y.Anim({
                    node: '#testDiv',
                    to: {
                        width: 400
                    },
                    duration: 3,
                    easing: Y.Easing.easeOut
            });

            //assign oncomplete handler
            myAnim.on("end", function(){

                //tell the TestRunner to resume
                this.resume(function(){

                    Y.Assert.areEqual(document.getElementById("testDiv").offsetWidth, 400, "Width of the DIV should be 400.");

                });

            }, this, true);

            //start the animation
            myAnim.run();

            //wait until something happens
            this.wait();

        }

    });

    //create the console
    var r = new Y.Console({
        newestOnTop : false,
        style: 'block' // to anchor in the example content
    });

    r.render('#testLogger');

    //create the logger
    Y.Test.Runner.add(Y.example.test.AsyncTestCase);

    //run the tests
    Y.Test.Runner.run();
});

</script>

</div>

<h2 class="first" id="asynchronous-events-test-example">Asynchronous Events Test Example</h2>

<p>This example begins by creating a namespace:</p>
<pre class="code prettyprint">Y.namespace(&quot;example.test&quot;);</pre>

<p>This namespace serves as the core object upon which others will be added (to prevent creating global objects).</p>

<h3 id="creating-the-testcase">Creating the TestCase</h3>

<p>The first step is to create a new <code>Y.Test.Case</code>object called <code>AsyncTestCase</code>.
  To do so, using the <code>Y.Test.Case</code>constructor and pass in an object literal containing information about the tests to be run:</p>
<pre class="code prettyprint">Y.example.test.AsyncTestCase = new Y.Test.Case({

    &#x2F;&#x2F;name of the test case - if not provided, one is auto-generated
    name : &quot;Animation Tests&quot;,

    &#x2F;&#x2F;---------------------------------------------------------------------
    &#x2F;&#x2F; Test methods - names must begin with &quot;test&quot;
    &#x2F;&#x2F;---------------------------------------------------------------------

    testAnimation : function (){

        var myAnim = new Y.Anim({
                node: &#x27;#testDiv&#x27;,
                to: {
                    width: 400
                },
                duration: 3,
                easing: Y.Easing.easeOut
        });

        &#x2F;&#x2F;assign oncomplete handler
        myAnim.on(&quot;end&quot;, function(){

            &#x2F;&#x2F;tell the TestRunner to resume
            this.resume(function(){

                Y.Assert.areEqual(document.getElementById(&quot;testDiv&quot;).offsetWidth, 400, &quot;Width of the DIV should be 400.&quot;);

            });

        }, this, true);

        &#x2F;&#x2F;start the animation
        myAnim.run();

        &#x2F;&#x2F;wait until something happens
        this.wait();

    }

});</pre>

<p>The only test in the <code>Y.Test.Case</code>is called <code>testAnimation</code>. It begins by creating a new
<code>Anim</code> object that will animate the width of a <code>div</code> to 400 pixels over three seconds. An
event handler is assigned to the <code>Anim</code> object's <code>end</code> event, within which the
<code>resume()</code> method is called. A function is passed into the <code>resume()</code> method to indicate
the code to run when the test resumes, which is a test to make sure the width is 400 pixels. After that, the
<code>run()</code> method is called to begin the animation and the <code>wait()</code> method is called to
tell the <code>Y.Test.Runner</code> to wait until it is told to resume testing again. When the animation completes,
the <code>end</code> event is fired and the test resumes, assuring that the width is correct.</p>
<h3 id="running-the-tests">Running the tests</h3>

<p>With all of the tests defined, the last step is to run them:</p>

<pre class="code prettyprint">&#x2F;&#x2F;create the console
var r = new Y.Console({
    verbose : true,
    newestOnTop : false
});

r.render(&#x27;#testLogger&#x27;);

&#x2F;&#x2F;create the logger
Y.Test.Runner.add(Y.example.test.AsyncTestCase);

&#x2F;&#x2F;run the tests
Y.Test.Runner.run();</pre>


<p>Before running the tests, it's necessary to create a <code>Y.Console</code> object to display the results (otherwise the tests would run
  but you wouldn't see the results). After that, the <code>Y.Test.Runner</code> is loaded with the <code>Y.Test.Case</code>object by calling
  <code>add()</code> (any number of <code>Y.Test.Case</code>and <code>TestSuite</code> objects can be added to a <code>Y.Test.Runner</code>,
  this example only adds one for simplicity). The very last step is to call <code>run()</code>, which begins executing the tests in its
  queue and displays the results in the <code>Y.Console</code>.</p>

<h2 id="complete-example-source">Complete Example Source</h2>

<pre class="code prettyprint">&lt;div id=&quot;testLogger&quot;&gt;&lt;&#x2F;div&gt;
&lt;div id=&quot;testDiv&quot; style=&quot;left:0;position:absolute;width:10px;height:10px; background-color:red&quot;&gt;&lt;&#x2F;div&gt;

&lt;script&gt;
YUI().use(&#x27;anim&#x27;, &#x27;console&#x27;, &#x27;test&#x27;, function (Y) {

    Y.namespace(&quot;example.test&quot;);

    Y.example.test.AsyncTestCase = new Y.Test.Case({

        &#x2F;&#x2F;name of the test case - if not provided, one is auto-generated
        name : &quot;Animation Tests&quot;,

        &#x2F;&#x2F;---------------------------------------------------------------------
        &#x2F;&#x2F; Test methods - names must begin with &quot;test&quot;
        &#x2F;&#x2F;---------------------------------------------------------------------

        testAnimation : function (){

            var myAnim = new Y.Anim({
                    node: &#x27;#testDiv&#x27;,
                    to: {
                        width: 400
                    },
                    duration: 3,
                    easing: Y.Easing.easeOut
            });

            &#x2F;&#x2F;assign oncomplete handler
            myAnim.on(&quot;end&quot;, function(){

                &#x2F;&#x2F;tell the TestRunner to resume
                this.resume(function(){

                    Y.Assert.areEqual(document.getElementById(&quot;testDiv&quot;).offsetWidth, 400, &quot;Width of the DIV should be 400.&quot;);

                });

            }, this, true);

            &#x2F;&#x2F;start the animation
            myAnim.run();

            &#x2F;&#x2F;wait until something happens
            this.wait();

        }

    });

    &#x2F;&#x2F;create the console
    var r = new Y.Console({
        newestOnTop : false,
        style: &#x27;block&#x27; &#x2F;&#x2F; to anchor in the example content
    });

    r.render(&#x27;#testLogger&#x27;);

    &#x2F;&#x2F;create the logger
    Y.Test.Runner.add(Y.example.test.AsyncTestCase);

    &#x2F;&#x2F;run the tests
    Y.Test.Runner.run();
});

&lt;&#x2F;script&gt;</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            
                <div id="toc" class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Table of Contents</h2>
                    </div>

                    <div class="bd">
                        <ul class="toc">
<li>
<a href="#asynchronous-events-test-example">Asynchronous Events Test Example</a>
<ul class="toc">
<li>
<a href="#creating-the-testcase">Creating the TestCase</a>
</li>
<li>
<a href="#running-the-tests">Running the tests</a>
</li>
</ul>
</li>
<li>
<a href="#complete-example-source">Complete Example Source</a>
</li>
</ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Demonstrates basic usage of YUI Test for setting up and running tests.">
                                        <a href="test-simple-example.html">Simple Testing Example</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Demonstrates how to use advanced testing features such as defining tests that should fail, tests that should be ignored, and tests that should throw an error.">
                                        <a href="test-advanced-test-options.html">Advanced Test Options</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Demonstrates how to use the ArrayAssert object to test array data.">
                                        <a href="test-array-tests.html">Array Processing</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Demonstrates basic asynchronous tests.">
                                        <a href="test-async-test.html">Asynchronous Testing</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Demonstrates using events with asynchronous tests.">
                                        <a href="test-async-event-tests.html">Asynchronous Event Testing</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
